/**
 * Created by kangm_000 on 2016/12/6 0006.
 */
var vm = new Vue({
    el: '#main',
    data: {
        menuNodes: [],
        zTree: {},
        rMenu: {},
        setting: {},
        currentMenu: {},
        parentMenu: {}
    },
    created: function () {
        this.setting = {
            data: {
                simpleData: {
                    enable: true,
                    idKey: "menuNo",
                    pIdKey: "parentNo"
                },
                key: {
                    name: "menuName"
                }
            },
            view: {
                dblClickExpand: false,
                showLine: true,
                showIcon: true,
                selectedMulti: false
            },
            callback: {
                onRightClick: this.OnRightClick
            }
        };

        App.ajax({
			type:"GET",
			url:App.getApiUrl("queryMenuPageURL"),
			success:function(data){
				vm.menuNodes = data;

                $.fn.zTree.init($("#menuTree"), vm.setting, vm.menuNodes);
                vm.zTree = $.fn.zTree.getZTreeObj("menuTree");
                vm.rMenu = $("#rMenu");

                var nodes = vm.zTree.getNodes();
                if (nodes.length > 0) {
                    var node = vm.zTree.selectNode(nodes[0]);
                    //vm.parentMenu = nodes[0];
                    vm.setMenuNodeInfo(nodes[0], {});
                    resizeLeft();
                }
			}
		});
    },
    computed: {},
    methods: {
        OnRightClick: function (event, treeId, treeNode) {
            if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
                //this.zTree.cancelSelectedNode();
                //this.showRMenu("root", event.clientX, event.clientY);
                //this.zTree.selectNode(treeNode);
                //this.showRMenu("root", event.clientX - 20, event.clientY - 20);
            } else if (treeNode && treeNode.parentNo == null) {
                this.zTree.selectNode(treeNode);
                this.showRMenu("root", event.clientX - 20, event.clientY - 20);
            } else if (treeNode && !treeNode.noR) {
                this.zTree.selectNode(treeNode);
                this.showRMenu("node", event.clientX - 20, event.clientY - 20);
            }
        },
        showRMenu: function (type, x, y) {
            $("#rMenu ul").show();
            if (type == "root") {
                $("#m_modify").hide();
                $("#m_del").hide();
            } else {
                $("#m_modify").show();
                $("#m_del").show();
            }
            this.rMenu.css({"top": y + "px", "left": x + "px", "visibility": "visible"});
            $("body").bind("mousedown", this.onBodyMouseDown);
        },
        hideRMenu: function () {
            if (this.rMenu) this.rMenu.css({"visibility": "hidden"});
            $("body").unbind("mousedown", this.onBodyMouseDown);
        },
        addMenuNode: function () {
            this.hideRMenu();
            if (this.zTree.getSelectedNodes()[0]) {
                this.setMenuNodeInfo(this.zTree.getSelectedNodes()[0], {});
                resizeLeft();
            }
        },
        modifyMenuNode: function () {
            this.hideRMenu();
            if (this.zTree.getSelectedNodes()[0]) {
                this.setMenuNodeInfo(this.zTree.getSelectedNodes()[0].getParentNode(), this.zTree.getSelectedNodes()[0]);
                resizeLeft();
            }
        },
        removeMenuNode: function () {
        	parent.showConfirm(
            	"提示信息",
            	"确定要删除该节点菜单吗？",
            	function(){
				   vm.hideRMenu();
				            var nodes = vm.zTree.getSelectedNodes();
				            if (nodes && nodes.length > 0) {
				                if (nodes[0].children && nodes[0].children.length > 0) {
				                    var msg = "要删除的节点是父节点，如果删除将连同子节点一起删掉。\n\n请确认！";
				        if (confirm(msg) == true) {
				            vm.deleteMenu(nodes[0]);
				        }
				    } else {
				        vm.deleteMenu(nodes[0]);
				    }
				}           		
            	}        		
        	);
          
        },
        setMenuNodeInfo: function (parent, current) {
            this.parentMenu = parent;
            this.currentMenu = current;
            setTimeout(function () {
                $('#menuType,#menuStatus').trigger('changed.selected.amui');
            }, 1);
        },
        onBodyMouseDown: function (event) {
            if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
                this.rMenu.css({"visibility": "hidden"});
            }
        },
        addMenu: function () {
            vm.currentMenu.parentNo = vm.parentMenu.menuNo;
            vm.currentMenu.menuSeq = vm.parentMenu.menuSeq;
    
            if(!this.currentMenu.menuName) {
				parent.showAlert("菜单名称不能为空")
				return;
			}
            if(!this.currentMenu.parentNo) {
				parent.showAlert("上级菜单不能为空")
				return;
			}
            if(!this.currentMenu.menuOrder) {
				parent.showAlert("菜单显示顺序不能为空")
				return;
			}
            if(!this.currentMenu.menuStatus) {
				parent.showAlert("菜单状态不能为空")
				return;
			}
            if(!this.currentMenu.menuType) {
				parent.showAlert("菜单类型不能为空")
				return;
			}
            if(this.currentMenu.menuFlag) {
            	this.currentMenu.menuFlag='1';
			}else{
				this.currentMenu.menuFlag='0';
			}
            App.ajax({
    			type:"POST",
    			url:App.getApiUrl("addMenuURL"),
    			data: vm.currentMenu,
    			success:function(data){
    				parent.showAlert(data.msg,function(){
                		window.location.reload();
                	});
    			}
    		});
        },
        resetMenu: function () {
            var newMenu = {
                menuNo:vm.currentMenu.menuNo,
                parentNo: vm.parentMenu.menuNo,
                menuSeq:vm.parentMenu.menuSeq
            };
            this.setMenuNodeInfo(vm.parentMenu, newMenu);
        },
        updateMenu: function () {
        	if(!this.currentMenu.menuNo) {
				parent.showAlert("菜单编号不能为空")
				return;
			}
            if(!this.currentMenu.menuName) {
				parent.showAlert("菜单名称不能为空")
				return;
			}
            if(!this.currentMenu.parentNo) {
				parent.showAlert("上级菜单不能为空")
				return;
			}
            if(!this.currentMenu.menuOrder) {
				parent.showAlert("菜单显示顺序不能为空")
				return;
			}
            if(!this.currentMenu.menuStatus) {
				parent.showAlert("菜单状态不能为空")
				return;
			}
            if(!this.currentMenu.menuType) {
				parent.showAlert("菜单类型不能为空")
				return;
			}
            if(this.currentMenu.menuFlag) {
            	this.currentMenu.menuFlag='1';
			}else{
				this.currentMenu.menuFlag='0';
			}
            App.ajax({
    			type:"POST",
    			url:App.getApiUrl("updateMenuURL"),
    			data: {
                    menuNo: vm.currentMenu.menuNo,
                    menuName: vm.currentMenu.menuName,
                    menuUrl: vm.currentMenu.menuUrl,
                    parentNo: vm.currentMenu.parentNo,
                    menuOrder: vm.currentMenu.menuOrder,
                    menuType: vm.currentMenu.menuType,
                    menuStatus: vm.currentMenu.menuStatus,
                    menuFlag: vm.currentMenu.menuFlag,
                },
    			success:function(data){
    				parent.showAlert(data.msg,function(){
                		window.location.reload();
                	});
    			}
    		});
        },
        deleteMenu: function (node) {
        	App.ajax({
    			type:"POST",
    			url:App.getApiUrl("deleteMenuURL"),
    			data: {
                    menuNo: node.menuNo
                },
    			success:function(data){
    				parent.showAlert(data.msg,function(){
                		vm.zTree.removeNode(node);
                	});
    			}
    		});
        }
    }
});

function resizeLeft() {
    setTimeout(function () {
        var minHeight = $(".am-u-lg-9").height() - 20;
        $(".am-u-lg-3").css("min-height", minHeight);
        $(".am-u-lg-3 .widget-body-lg").css("min-height", minHeight);
    }, 1);
}

/*****添加select事件******/
$(function () {
    $('#menuType').on('change', function () {
        vm.currentMenu.menuType = $(this).val();
    });

    $('#menuStatus').on('change', function () {
        vm.currentMenu.menuStatus = $(this).val();
    });
});